package com.ebraga.trackerclub.android.manager.service;

import java.io.InputStream;

import org.apache.commons.lang.StringUtils;

import android.content.res.AssetManager;

import com.ebraga.trackerclub.android.dao.api.ParameterDao;
import com.ebraga.trackerclub.android.dao.api.PositionDao;
import com.ebraga.trackerclub.android.dao.api.RankingDao;
import com.ebraga.trackerclub.android.dao.api.UserDao;
import com.ebraga.trackerclub.android.dao.api.webservice.ParameterWSDao;
import com.ebraga.trackerclub.android.dao.api.webservice.PositionWSDao;
import com.ebraga.trackerclub.android.dao.api.webservice.RankingWSDao;
import com.ebraga.trackerclub.android.dao.api.webservice.UserWSDao;
import com.ebraga.trackerclub.android.dao.entity.UserAndroid;
import com.ebraga.trackerclub.android.dao.service.ParameterServiceDao;
import com.ebraga.trackerclub.android.dao.service.PositionServiceDao;
import com.ebraga.trackerclub.android.dao.service.RankingServiceDao;
import com.ebraga.trackerclub.android.dao.service.SyncDataServiceDao;
import com.ebraga.trackerclub.android.dao.service.UserServiceDao;
import com.ebraga.trackerclub.android.dao.service.webservice.ParameterWSServiceDAO;
import com.ebraga.trackerclub.android.dao.service.webservice.PositionWSServiceDAO;
import com.ebraga.trackerclub.android.dao.service.webservice.RankingWSServiceDAO;
import com.ebraga.trackerclub.android.dao.service.webservice.UserWSServiceDAO;
import com.ebraga.trackerclub.android.exception.BackofficeDBConnectionException;
import com.ebraga.trackerclub.android.exception.BackofficeUtilException;
import com.ebraga.trackerclub.android.exception.dao.BackofficeDaoException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeManagerException;
import com.ebraga.trackerclub.android.manager.core.SyncDataManagerImpl;
import com.ebraga.trackerclub.android.util.ISmartServiceConstants;
import com.ebraga.trackerclub.android.util.api.AndroidContext;
import com.ebraga.trackerclub.android.util.core.AndroidContentValuesImpl;
import com.ebraga.trackerclub.android.util.core.DataAccessObjectImpl;
import com.ebraga.trackerclub.android.util.webservice.HTTPSAndroidClientWrapper;
import com.ebraga.trackerclub.rest.dao.entity.webservice.PositionWSEntity;
import com.ebraga.trackerclub.rest.dao.entity.webservice.UserWSEntity;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class SyncDataServiceManager extends SyncDataManagerImpl {
	
	

	public SyncDataServiceManager(AndroidContext context) throws BackofficeManagerException {
		
		String enviroment = System.getProperty("Enviroment");
		if( !"UNIT_TEST".equals(enviroment) ) {
			try {
				final DataAccessObjectImpl dataAccessObject = new DataAccessObjectImpl(context);
				ParameterServiceDao parameterDao = new ParameterServiceDao(dataAccessObject);
				parameterDao.setContentValues(new AndroidContentValuesImpl());
				setParameterDao(parameterDao);
				parameterDao.setContentValues(new AndroidContentValuesImpl());
				
				UserServiceDao userDao = new UserServiceDao(dataAccessObject); 
				userDao.setContentValues(new AndroidContentValuesImpl());
				setUserDao(userDao);				
				
				PositionServiceDao positionDao = new PositionServiceDao(dataAccessObject); 
				positionDao.setContentValues(new AndroidContentValuesImpl());
				setPositionDao(positionDao);
				
				RankingServiceDao rankingDao = new RankingServiceDao(dataAccessObject); 
				rankingDao.setContentValues(new AndroidContentValuesImpl());
				setRankingDao(rankingDao);				
				
				SyncDataServiceDao syncDataDao = new SyncDataServiceDao(dataAccessObject);
				syncDataDao.setContentValues(new AndroidContentValuesImpl());
				setSyncDataDao(syncDataDao);
				
				super.setUserDao(new UserServiceDao(dataAccessObject));
				
				HTTPBasicAuthFilter basicAuth = null;
				
				String keyStorePasswordP12 	= ISmartServiceConstants.WEBSERVICE_CERTIFICATE.KEY_STORE_PASSWORD;
				
				String keyStorePasswordJks 			= ISmartServiceConstants.WEBSERVICE_CERTIFICATE.JKS_PASSWORD;
				
				try {
					basicAuth = buildHTTPBasicAuthFilter(super.getUserDao().lastLogin());
				} catch (BackofficeDBConnectionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (BackofficeDaoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				final ParameterWSServiceDAO parameterWSServiceDAO = new ParameterWSServiceDAO();
				parameterWSServiceDAO.setHTTPSClientWrapper(new HTTPSAndroidClientWrapper<String>());
				parameterWSServiceDAO.setURL(ISmartServiceConstants.WEBSERVICE_URL.RETRIEVE_PARAMETERS_URL);
				setParameterWSDAO(parameterWSServiceDAO);
				
				final UserWSServiceDAO userWSServiceDAO = new UserWSServiceDAO();
				userWSServiceDAO.setHTTPSClientWrapper(new HTTPSAndroidClientWrapper<UserWSEntity>());
				userWSServiceDAO.setURL(ISmartServiceConstants.WEBSERVICE_URL.INSERT_USER_URL);
				setUserWSDAO(userWSServiceDAO);				
				
				final PositionWSServiceDAO positionWSServiceDAO = new PositionWSServiceDAO();
				positionWSServiceDAO.setHTTPSClientWrapper(new HTTPSAndroidClientWrapper<PositionWSEntity>());
				positionWSServiceDAO.setURL(ISmartServiceConstants.WEBSERVICE_URL.INSERT_POSITIONS_URL);
				setPositionWSDAO(positionWSServiceDAO);
				
				final RankingWSServiceDAO rankingWSServiceDAO = new RankingWSServiceDAO();
				rankingWSServiceDAO.setHTTPSClientWrapper(new HTTPSAndroidClientWrapper<String>());
				rankingWSServiceDAO.setURL(ISmartServiceConstants.WEBSERVICE_URL.RETRIEVE_RANKING_URL);
				setRankingWSDAO(rankingWSServiceDAO);				
				
			} catch (BackofficeUtilException e) {
				throw new BackofficeManagerException(e);
			} 
		}
	}
	
	private static HTTPBasicAuthFilter buildHTTPBasicAuthFilter(UserAndroid user) {
		String imei = StringUtils.rightPad(user.getImei(), 20, ' ');		

		String password = imei;
		
		return new HTTPBasicAuthFilter(user.getImei(), password);
		
	}
	

	private InputStream loadCertificateDataP12(AndroidContext context) {
		return loadCertificateData(context, "sistema_geolocalizacao.p12");
	}

	
	private InputStream loadCertificateDataJks(AndroidContext context) {
		return loadCertificateData(context, "backoffice_proxy.bks");
	}
    	
	
	private InputStream loadCertificateData(AndroidContext context, String certName) {
		
		InputStream certificateStream = null;
		
		if (context == null) {
			return null;
		}
		
		try {		
			AssetManager assetManager = context.getAssets();
			certificateStream = assetManager.open(certName);
		
		} catch (Exception e) {
			// 
		}

		return certificateStream;
	}

	public void setParameterDao(ParameterDao parameterDao) {
		super.setParameterDao(parameterDao);
	}
	
	public void setPositionDao(PositionDao positionDao) {
		super.setPositionDao(positionDao);
	}
	
	public void setRankingDao(RankingDao rankingDao) {
		super.setRankingDao(rankingDao);
	}	
	
	public void setUserDao(UserDao userDao) {
		super.setUserDao(userDao);
	}	

	public void setParameterWSDAO(ParameterWSDao parameterWSDAO) {
		super.setParameterWSDAO(parameterWSDAO);
	}
	
	public void setRankingWSDAO(RankingWSDao rankingWSDAO) {
		super.setRankingWSDAO(rankingWSDAO);
	}	

	public void setPositionWSDAO(PositionWSDao positionWSDAO) {
		super.setPositionWSDAO(positionWSDAO);
	}
	
	public void setUserWSDAO(UserWSDao userWSDAO) {
		super.setUserWSDAO(userWSDAO);
	}	
}
